package DBOpt;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.*;

public class ResultMerge {
	private HashSet<String> KnownErrorSet;
	
	protected ResultMerge()
	{
		
	}

	private void process(String resultFile, String errorFile) throws Exception
	{
		  SimpleDateFormat dashDateFormat = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
		  SimpleDateFormat nodashDateFormat = new SimpleDateFormat("yyyyMMdd",Locale.US);
		  
		  FileWriter outputfile = new FileWriter ("..//output/meas/AnalysisResult.txt");
		  System.out.println("creat outpufile file");
		  //outputfile.write("***  Start ***\n");
		  FileReader fr = new FileReader(errorFile);
		  BufferedReader br = new BufferedReader(fr);
		  String line;
		  TreeMap <Date,Integer> errormap = new TreeMap <Date,Integer> ();
		   while(true)
	       {
	    	   line = br.readLine();
	    	   if (line == null) break;
	           if (line.length()==0) continue;
	           int tabpos = line.indexOf('\t');
	           if (tabpos <=0) continue;
	           String dateStr = line.substring(0, tabpos).trim();
	           
	           int tabpos2 = line.lastIndexOf('\t');
	           if (tabpos2 <=0) continue;
	           String errornumStr = line.substring(tabpos2,line.length()).trim();
	           String errormsg = line.substring(tabpos,tabpos2).trim();
	           if (!dateStr.matches(".*200[8-9][0-9][0-9][0-9][0-9].*")) continue;
	           int errornum = Integer.parseInt(errornumStr);
	           if (errornum <= 0)continue;
	           System.out.println("date: "+dateStr+"--error:--"+errormsg+"--error num:--"+errornum);
	           if (errormsg.indexOf("Total Lost Record")  < 0) continue;
	           Date date = nodashDateFormat.parse(dateStr);
	           errormap.put (date,errornum);
	    }   
		fr.close();
		br.close();
		
		FileReader fr2 = new FileReader(resultFile);
		BufferedReader br2 = new BufferedReader(fr2);
		boolean inMsgField = false;
		while(true)
	    {
	    	   line = br2.readLine();
	    	   if (line == null) break;
	    	   line = line.trim();
	           if (line.contains("#Msg")){
        		   outputfile.write(line+"\t#LostMsg\t#TotalMsg\n");
        		   inMsgField = true;
               }
	           else if (!inMsgField)
	           {
	        	   outputfile.write(line+"\n");
	           }
	           else
	           {
	        	
	        	   int tabpos = line.indexOf('\t');
		           if (tabpos <=0) 
		           {
		        	   inMsgField = false;
		        	   continue;
		           }
		           String dateStr = line.substring(0, tabpos).trim();
		           if (!dateStr.matches(".*200[8-9]-[0-9][0-9]-[0-9][0-9].*")) 
		           {
		        	   inMsgField = false;
		        	   continue;
		           }
		           Date date = dashDateFormat.parse(dateStr);
		           
		           int tabpos2 = line.lastIndexOf('\t');
		           if (tabpos2 <=0) 
		           {
		        	   inMsgField = false;
		        	   continue;
		           }
		           String numStr = line.substring(tabpos2,line.length()).trim();
		           int num = Integer.parseInt(numStr);
		           if (num <= 0)continue;
		           
		           Integer errornum = errormap.get(date);
		           System.out.println("date: "+dateStr+"--error num:--"+errornum);
		           
		           if (errornum != null)
		           {
		        	   Integer total = errornum+num;
		        	   outputfile.write(line+"\t"+errornum+"\t"+total+"\n");
		           }
		           else
		        	   outputfile.write(line+"\t"+0+"\t"+num+"\n");
	           }
	    }
		outputfile.close();
	}
	
	public static void main(String args[]) {
		   
		if (args.length < 2)
		{
			System.out.println("Usage: resultmerge <analysis result file> <error file>");
			System.exit(0) ;
		}
		try{  
		String resultFile = args[0];
		String errorFile = args[1];
		
		ResultMerge resultProcessor = new ResultMerge();
		resultProcessor.process( resultFile,errorFile);
		}
	      catch( Exception e ) 
	      {
	         e.printStackTrace();
	       }
	      System.out.println("Done!");
	  
	}


}
